/*
 * Copyright (C) 2010-2012 Geometer Plus <contact@geometerplus.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

package cn.dns.reader.network;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.model.BookListEntity.BookEntity;
import com.hctforgreen.greenservice.model.VideoTypeEntity.VideoDetailEntity.VideoPartEntity;
import com.hctforgreen.greenservice.utils.FileCacheUtil;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.Utils;
import com.mato.sdk.proxy.Proxy;

import java.io.File;
import java.util.List;

public abstract class Util {
	public final static String DISPATCH_APK_DOWNLOAD_URL = "http://mobileapi.gree.com/dispatch/AttachFiles/dispatch.apk";

	public static void openInBrowser(Activity activity, String url) {
		if (url != null) {
			activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri
					.parse(url)));
		}
	}
	/**
	 * 390694--下载管理中点击下载按钮下载资料
	 * @param activity
	 * @param book
	 */
	public static void doDownloadBook(final Activity activity,
			final BookEntity book) {
		if (book != null) {
			if (Utils.isWifiNetworkConnected(activity)) {
				doDownload(activity, book);
			} else {
				AlertDialog.Builder builder = new Builder(activity);
				builder.setTitle(
						activity.getString(R.string.dialog_default_title_hint))
						.setMessage(activity.getString(R.string.dialog_network_3g_wran))
						.setPositiveButton(activity.getString(R.string.dialog_confirm_hint),
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog, int which) {
										doDownload(activity, book);
									}
								})
						.setNegativeButton(activity.getString(R.string.dialog_cancel_hint), null).create().show();

				// doDownload(activity, book);
			}

		}
	}

	/**
	 * 390694-启动服务Service下载资料
	 * @param activity
	 * @param book
	 */
	public static void doDownload(final Activity activity, BookEntity book) {
		Uri uri = Uri.parse(book.download_url);
		final List<String> path = uri.getPathSegments();
		final String fileName = path.get(path.size() - 1).toLowerCase();
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(book.download_url), activity.getApplicationContext(),
				BookDownloaderService.class);
		intent.putExtra(BookDownloaderService.SHOW_NOTIFICATIONS_KEY, BookDownloaderService.Notifications.ALL);
		intent.putExtra(BookDownloaderService.BOOK_FORMAT_KEY, book.suffix);
		// intent.putExtra(BookDownloaderService.CLEAN_URL_KEY,
		// book.download_url);
		intent.putExtra(HctConstants.ON_ACTIVITY_KEY_BOOK_ENTITY, book);
		intent.putExtra(BookDownloaderService.TITLE_KEY, book.displayName);
		//判断是否是在下载管理下载书
		intent.putExtra("isFromDownloadManager", false);
		if (Proxy.getAddress() != null) {
			intent.putExtra("address.host", Proxy.getAddress().getHost());
			intent.putExtra("address.port", Proxy.getAddress().getPort());
		}

		activity.startService(intent);  
	}

	/**
	 * 390694--下载管理中点击下载按钮下载资料
	 * @param activity
	 * @param book
	 * 390778
	 * 2016-12-19
	 * 复制该方法 并增加状态参数
	 */
	public static void doDownloadBook(final Activity activity,
									  final BookEntity book,boolean state) {
		if (book != null) {
			if (Utils.isWifiNetworkConnected(activity)) {
				doDownload(activity, book,state);
			} else {
				AlertDialog.Builder builder = new Builder(activity);
				builder.setTitle(
						activity.getString(R.string.dialog_default_title_hint))
						.setMessage(
								activity.getString(R.string.dialog_network_3g_wran))
						.setPositiveButton(
								activity.getString(R.string.dialog_confirm_hint),
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
														int which) {
										doDownload(activity, book);
									}
								})
						.setNegativeButton(
								activity.getString(R.string.dialog_cancel_hint),
								null).create().show();

				// doDownload(activity, book);
			}

		}
	}

	/**
	 * 390694-启动服务Service下载资料
	 * @param activity
	 * @param book
	 * 390778
	 * 2016-12-19
	 * 复制该方法 并增加状态参数
	 */
	public static void doDownload(final Activity activity, BookEntity book,boolean state) {
		Uri uri = Uri.parse(book.download_url);
		final List<String> path = uri.getPathSegments();
		final String fileName = path.get(path.size() - 1).toLowerCase();

		Intent intent = new Intent(Intent.ACTION_VIEW,
				Uri.parse(book.download_url), activity.getApplicationContext(),
				BookDownloaderService.class);
		intent.putExtra(BookDownloaderService.SHOW_NOTIFICATIONS_KEY,
				BookDownloaderService.Notifications.ALL);
		intent.putExtra(BookDownloaderService.BOOK_FORMAT_KEY, book.suffix);
		// intent.putExtra(BookDownloaderService.CLEAN_URL_KEY,
		// book.download_url);
		intent.putExtra(HctConstants.ON_ACTIVITY_KEY_BOOK_ENTITY, book);
		intent.putExtra(BookDownloaderService.TITLE_KEY, book.displayName);
		intent.putExtra("updateDateServicestate", state);
		//判断是否是在下载管理下载书
		intent.putExtra("isFromDownloadManager", true);
		if (Proxy.getAddress() != null) {
			intent.putExtra("address.host", Proxy.getAddress().getHost());
			intent.putExtra("address.port", Proxy.getAddress().getPort());
		}

		activity.startService(intent);
	}
	/**
	 * 下载apk
	 * 
	 * @param activity
	 * @param url
	 *            下载地址
	 * @param title
	 *            "" 为格力掌上通
	 */
	public static void dowloadApk(final Activity activity, String url,
			String title) {
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url),
				activity.getApplicationContext(), UpdateVersionService.class);
		intent.putExtra(UpdateVersionService.SHOW_NOTIFICATIONS_KEY,
				UpdateVersionService.Notifications.ALL);
		intent.putExtra(UpdateVersionService.TITLE_KEY,title.equals("")?"格力掌上通":
				activity.getString(R.string.gree_dispatch_name));
		if (Proxy.getAddress() != null) {
			intent.putExtra("address.host", Proxy.getAddress().getHost());
			intent.putExtra("address.port", Proxy.getAddress().getPort());
		}
		activity.startService(intent);
	}

	public static void tryToDeleteBookZip(final BookEntity book, Context context) {
		try {
			String local = BookEntity.makeBookFileName(book.download_url,
					book.suffix, context);
			if (local != null) {
				File file = new File(local);
				file.delete();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void tryToDeleteBook(final BookEntity book, Context context) {
		try {
			String local = BookEntity.makeBookFileName(book.download_url,
					book.suffix, context);
			
			if (local != null) {
				File file = new File(local);
				file.delete();

				FileCacheUtil cacheUtil = new FileCacheUtil();
				cacheUtil.deleteFileDirectory(local.replace(".zip", ""));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 下载视频
	 * 
	 * @param activity
	 * @param book
	 */
	public static void doDownloadVideo(final Activity activity,
			final VideoPartEntity book) {
		if (book != null) {
			if (!Utils.isNetworkConnected(activity)) {
				Toast.makeText(activity,
						activity.getString(R.string.net_error_hint),
						Toast.LENGTH_LONG).show();
				return;
			}

			if (Utils.isWifiNetworkConnected(activity)) {
				doDownload(activity, book);
			} else {
				AlertDialog.Builder builder = new Builder(activity);
				builder.setTitle(
						activity.getString(R.string.dialog_default_title_hint))
						.setMessage(
								activity.getString(R.string.dialog_network_3g_wran))
						.setPositiveButton(
								activity.getString(R.string.dialog_confirm_hint),
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										Utils.submitPoints(activity, 2, "",
												false);
										doDownload(activity, book);
									}
								})
						.setNegativeButton(
								activity.getString(R.string.dialog_cancel_hint),
								null).create().show();

				// doDownload(activity, book);
			}

		}
	}

	/**
	 * 下载资料
	 * 
	 * @param activity
	 * @param book
	 */
	public static void doDownload(final Activity activity, VideoPartEntity book) {
		// Uri uri = Uri.parse(book.videoUrl);
		// final List<String> path = uri.getPathSegments();
		// final String fileName = path.get(path.size() - 1).toLowerCase();

		Intent intent = new Intent(Intent.ACTION_VIEW,
				Uri.parse(book.videoUrl), activity.getApplicationContext(),
				VideoDownloaderService.class);
		intent.putExtra(VideoDownloaderService.SHOW_NOTIFICATIONS_KEY,
				VideoDownloaderService.Notifications.ALL);
		// intent.putExtra(BookDownloaderService.CLEAN_URL_KEY,
		// book.download_url);
		intent.putExtra(HctConstants.ON_ACTIVITY_KEY_BOOK_ENTITY, book);
		intent.putExtra(VideoDownloaderService.TITLE_KEY, book.videoName);
		if (Proxy.getAddress() != null) {
			intent.putExtra("address.host", Proxy.getAddress().getHost());
			intent.putExtra("address.port", Proxy.getAddress().getPort());
		}
		activity.startService(intent);
	}

}
